package com.admin.util;


import com.admin.base.SysConstant;
import com.admin.bean.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>Session工具类</p>
 *
 * @Author: Hanson
 * @time 2019/8/9 19:17
 */
public class SessionUtil {

    /**
     * 获取request
     * @return
     */
    public static HttpServletRequest getRequest(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }

    /**
     * 获取session
     * @return
     */
    public static HttpSession getSession(){
        HttpServletRequest request = getRequest();
        return request==null?null:request.getSession(false);
    }

    /**
     * 获取真实路径
     * @return
     */
    public static String getRealRootPath(){
        HttpServletRequest request = getRequest();
        return request==null?null:request.getServletContext().getRealPath("/");
    }

    /**
     * 获取上下文path
     * @return
     */
    public static String getContextPath(){
        HttpServletRequest request = getRequest();
        return request==null?null:request.getContextPath();
    }

    /**
     * 获取ip
     * @return
     */
    public static String getIp(){
        HttpServletRequest request = getRequest();
        return request==null?null:request.getRemoteAddr();
    }

    /**
     * 获取session中的Attribute
     * @param name
     * @return
     */
    public static Object getSessionAttribute(String name){
        HttpSession session = getSession();
        return session==null?null:session.getAttribute(name);
    }

    /**
     * 设置session中的Attribute
     * @param name
     * @param value
     */
    public static void setSessionAttribute(String name, Object value){
        HttpSession session = getSession();
        if (session!=null){
            session.setAttribute(name, value);
        }
    }

    /**
     * 删除session中的Attribute
     * @param name
     */
    public static void removeSessionAttribute(String name){
        HttpSession session = getSession();
        if (session!=null){
            session.removeAttribute(name);
        }
    }

    /**
     * 获取request中的属性
     * @param name
     * @return
     */
    public static Object getRequestAttribute(String name){
        HttpServletRequest request = getRequest();
        return request==null?null:request.getAttribute(name);
    }

    /**
     * 设置request中的Attribute
     * @param name
     * @param value
     */
    public static void setRequestAttribute(String name, Object value){
        HttpServletRequest request = getRequest();
        if (request!=null){
            request.setAttribute(name, value);
        }
    }

    /**
     * 删除request中的Attribute
     * @param name
     */
    public static void removeRequestAttribute(String name){
        HttpServletRequest request = getRequest();
        if (request!=null){
            request.removeAttribute(name);
        }
    }

    /**
     * 获取当前登录用户
     * @return
     */
    public static User getCurrentUser(){
        Object sessionAttribute = getSessionAttribute(SysConstant.SESSION_USER);
        return sessionAttribute==null?null:(User)sessionAttribute;
    }

    /**
     * 设置当前登录用户
     * @param user
     */
    public static void setCurrentUser(User user){
        setSessionAttribute(SysConstant.SESSION_USER, user);
    }
}
